package com.somsoft.bzfk.common;

import java.io.IOException;
import java.io.InputStream;
import java.util.Random;

import org.apache.http.util.EncodingUtils;
import org.json.JSONArray;

import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.View;


public class Utils {

	static String[][] strfks = {{"111000000","100100100","111000000","100100100"},//长条
			{"110110000","110110000","110110000","110110000"},//方块
			{"100000000","100000000","100000000","100000000"},//小方块
			{"010111000","010110010","111010000","100110100"},//T型
			{"110010011","001111100","110010011","001111100"},//大Z型
			{"011010110","100111001","011010110","100111001"},//大反Z
			{"110011000","010110100","110011000","010110100"},//Z型
			{"011110000","100110010","011110000","100110010"},//反Z
			{"100100110","001111000","110010010","111100000"},//L型
			{"010010110","111001000","110100100","100111000"}};//反L
	
	public static Bitmap getImageFromAssetsFile(View v ,String fileName) {
		Bitmap image = null;
		AssetManager am = v.getResources().getAssets();
		try {
			InputStream is = am.open(fileName);
			image = BitmapFactory.decodeStream(is);
			is.close();
		} catch (IOException e) {
			Log.e("读取图片资源出错", "读取图片资源出错", e);
		}
		return image;
	}
	    
	
	public static JSONArray getMapFromAssetsFile(View v ,String fileName) {
		String json = null;
		JSONArray array = null;
		AssetManager am = v.getResources().getAssets();
		try {
			InputStream in = am.open(fileName);  
            //获取文件的字节数  
            int lenght = in.available();  
            //创建byte数组  
            byte[]  buffer = new byte[lenght];  
            //将文件中的数据读到byte数组中  
            in.read(buffer);  
            json = EncodingUtils.getString(buffer, "UTF-8");  
            array = new JSONArray(json);
		} catch (Exception e) {
			Log.e("读取地图出错", "读取地图出错", e);
		}
		return array;
	}
	
	public static WaitingBlock randBlock(){
		Random r = new Random();
		int color = r.nextInt(8) + 1; // 生成1-5
		WaitingBlock wb = new WaitingBlock();
		int[][] fks = new int[3][3];
		int y0 = r.nextInt(strfks.length);
		int x0 = r.nextInt(4);
		
		String strfk = strfks[y0][x0];
		int x=0,y=0;
		//解析字符串，使其变成方块数组
		for (int i = 0; i < strfk.length(); i++) {
			x = i % 3;
			y = i / 3;
			String str = strfk.substring(i, i + 1);
			if ("1".equals(str)) {
				fks[y][x] = color;
			} else {
				fks[y][x] = -1;
			}
		}
		wb.setFks(fks);
		wb.setX(x0);
		wb.setY(y0);
		wb.setColor(color);
		return wb;
	}
	
	public static WaitingBlock getNextBlock(int x0,int y0,int color){
		WaitingBlock wb = new WaitingBlock();
		int[][] fks = new int[3][3];
		int tmpX0 = (x0+1)>=4?0:(x0+1);
		String strfk = strfks[y0][tmpX0];
		int x=0,y=0;
		//解析字符串，使其变成方块数组
		for (int i = 0; i < strfk.length(); i++) {
			x = i % 3;
			y = i / 3;
			String str = strfk.substring(i, i + 1);
			if ("1".equals(str)) {
				fks[y][x] = color;
			} else {
				fks[y][x] = -1;
			}
		}
		wb.setFks(fks);
		wb.setX(tmpX0);
		wb.setY(y0);
		wb.setColor(color);
		return wb;
	}
	
}









